﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BlackBird.Models;

namespace BlackBird.DataAccess
{
	/// <summary>
	/// Provide a buffer support for all not changed object
	/// </summary>
	public static class GlobalObjectBuffer
	{
		public static List<user> allUsers = new List<user>();
		public static List<group> allGroups = new List<group>();
		public static Dictionary<string, List<group>> UserIdToGroups = new Dictionary<string, List<group>>();
		public static List<action> allActions = new List<action>();
		public static List<status> allStatus = new List<status>();
		public static List<event_type> allEventTypes = new List<event_type>();
        public static List<operation> allOperations = new List<operation>();


		//process
		public static List<processtype> allProcessTypes = new List<processtype>();
		public static List<process> allProcess = new List<process>();


		public static void Initialize()
		{
			AllProjects = new AllProjectsDM();
			using (var context = new blackbirdEntities())
			{
				allUsers = context.user.ToList();
				foreach (user us in allUsers)
				{
					var  id_groups = us.group_user.ToList();
					List<group> usergrouplist = new List<group>();
					foreach (var id_group in id_groups)
					{
						var gp = context.group.FirstOrDefault(v => v.idGroup == id_group.Group_idGroup);
						usergrouplist.Add(gp);
 					}
					UserIdToGroups.Add(us.idUser, usergrouplist);
				};
				allGroups = context.group.ToList();
				allActions = context.action.ToList();
				allStatus = context.status.ToList();
				allEventTypes = context.event_type.ToList();
                allOperations = context.operation.ToList();

				//process
				allProcessTypes = context.processtype.ToList();	
				allProcess = context.process.ToList();

				//project
				var projects = context.project;
				projects.ToList().ForEach(p => AllProjects.AllProjects.Add(BuildProject(p)));
			}

			//Initialize bi jiadan
			BiJiaDanDataAccess.InitializeAllBijiadans();
		}

		public static List<user> AllUsers
		{
			get
			{
				return allUsers;
			}
		}

		public static List<group> GetGroupByUserId(string userId)
		{
			if (!UserIdToGroups.Keys.Contains(userId))
				throw new KeyNotFoundException("user is invalid");

			return UserIdToGroups[userId];

		}

		public static List<group> AllGroups
		{
			get
			{
				return allGroups;
			}
		}

		public static List<action> AllActions
		{
			get
			{
				return allActions;
			}
		}

		public static List<status> AllStatus
		{
			get
			{
				return allStatus;
			}
		}

		public static List<event_type> AllEventTypes
		{
			get
			{
				return allEventTypes;
			}
		}

		//process
		public static List<processtype> AllProcessTypes
		{
			get
			{
				return allProcessTypes;
			}
		}


		public static List<process> AllProcess
		{
			get
			{
				return allProcess;
			}
		}


		/// <summary>
		/// All Project List
		/// </summary>
		public static AllProjectsDM AllProjects { get; set; }

		/// <summary>
		/// Build Project
		/// </summary>
		/// <param name="prj"></param>
		/// <returns></returns>
		public static ProjectDM BuildProject(project prj)
		{
			return new ProjectDM()
			{
				Id = prj.idProject.ToString(),
				//TODO: use the right image accroindg to Project type
				thumbnialurl = Links.Content.Images.project.example_PNG,
				ContactorInfo = DBUtils.ConvertDBToDM(prj.contactor),
				BasicInfo = new ProjectBasicDM()
				{
					Name = prj.Name,
					SuoXie = prj.SuoXie,
					Status = prj.status.DisplayName,
					CreateDate = prj.Date.ToString(),
					CreateUserName = prj.user.Name,

					//TODO: read the real data from db accrodingly
					//IncomeCount = 3,
					//ExpendCount = 4,
                    FuKuanDanCount = FuKuanDanDataAccess.AllFuKuanDanDMForCurrentProject(prj.idProject).Count,
					BiJiaDanCount = BiJiaDanDataAccess.GetAllBijiadansCount(prj.idProject)
				}
			};
		}
	}
}